#pragma once
#include<iostream>
#include<sys/epoll.h>
#include "Log.hpp"
#include "Comm.hpp"

namespace EpollModule
{
    using namespace LogModule;
    class Epoller
    {
    public:
        Epoller():_epfd(-1)
        {}
        void Init()
        {
            _epfd = epoll_create(256);
            if(_epfd<0)
            {
                LOG(LogLevel::ERROR)<<"epoll_create fail";
                exit(EPOLL_CREATE_ERR);
            }
            LOG(LogLevel::DEBUG)<<"epoll_create success";
        }   
        int Wait(struct epoll_event revs[], int num, int timeout) //输出就绪的fd和events
        {   
            int n = epoll_wait(_epfd, revs, num, timeout); //阻塞式
            if(n < 0)
            {
                LOG(LogLevel::WARING) << "epoll_wait error";
            }
            return n;
        }
        void Ctrl(int sockfd, uint32_t events)
        {
            struct epoll_event ev;
            ev.events = events;
            ev.data.fd = sockfd;
            int n = epoll_ctl(_epfd, EPOLL_CTL_ADD, sockfd, &ev);
            if(n < 0)
            {
                LOG(LogLevel::WARING)<<"epoll_ctl error";
            }
        }
        ~Epoller()
        {}
    private:
        int _epfd;
    };
};